﻿using Autofac;
using ThresholdAssistant.Tools;
using System;
using System.Linq;
using System.Reflection;
using AutofacModel = Autofac.Module;

namespace ThresholdAssistant.Autofac
{
    /// <summary>
    /// 默认注入
    /// </summary>
    public class DefaultModule : AutofacModel
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            //用于实现属性注入的基础接口
            Type baseType = typeof(IBaseAutofac);

            //扫描当前执行目录下所有存在继承于IAutofacBase接口的程序集
            Assembly[] assemblyList = AssemblyUtil.GetAssemblyList(baseType);
            if (assemblyList == null || assemblyList.Length <= 0)
                return;

            //注入所有继承于IAutofacBase接口的实现类
            builder.RegisterAssemblyTypes(assemblyList).Where(type => type != baseType && baseType.IsAssignableFrom(type))
                   .AsSelf().InstancePerLifetimeScope().PropertiesAutowired();
        }
    }
}